package com.zmn.brs.task.job;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.brs.business.interfaces.order.OrderComparisonBService;
import com.zmn.brs.business.interfaces.order.OrderQuantityBService;
import com.zmn.brs.business.interfaces.rule.AlarmRuleBService;
import com.zmn.brs.common.constant.TaskConst;
import com.zmn.brs.common.enums.EventTypeEnum;
import com.zmn.manager.lts.interfaces.task.TaskManager;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 描述： 订单人单比监控规则定时任务
 *
 * @author chongw
 * @since 2021-06-28 16:18
 */
@LTS
@Slf4j
public class OrderRuleJob {

    private static final String TAG = "订单人单比监控规则定时任务";

    @Resource
    private OrderComparisonBService orderComparisonBService;

    @Resource
    private OrderQuantityBService orderQuantityBService;

    @Resource
    private AlarmRuleBService alarmRuleBService;

    @Resource
    private TaskManager taskManager;

    @PostConstruct
    public void init() {
        //初始化缓存监控规则
        alarmRuleBService.initCacheRule();
        log.info("[{}], 定时任务初始化提交", TAG);
        Job comparisonJob = new Job();
        comparisonJob.setTaskId(TaskConst.TASK_ID_BRS_DATA_COMPARISON);
        comparisonJob.setCronExpression("0 */7 * * * ?");
        comparisonJob.setReplaceOnExist(true);
        comparisonJob.setParam(TaskManager.SHARD_FIELD, TaskConst.TASK_ID_BRS_DATA_COMPARISON);
        taskManager.submitTask2(comparisonJob);

        Job quantityPush = new Job();
        quantityPush.setTaskId(TaskConst.TASK_ID_BRS_DATA_QUANTITY_PUSH);
        quantityPush.setCronExpression("0 */30 * * * ?");
        quantityPush.setReplaceOnExist(true);
        quantityPush.setParam(TaskManager.SHARD_FIELD, TaskConst.TASK_ID_BRS_DATA_QUANTITY_PUSH);
        taskManager.submitTask2(quantityPush);
        log.info("[{}], 定时任务初始化成功......", TAG);
    }


    /**
     * 描述: 人单比任务初始化
     * @param job 任务
     * @return com.github.ltsopensource.tasktracker.Result
     * @author chongw
     * @since 2021/06/28 16:12
     */
    @JobRunnerItem(shardValue = TaskConst.TASK_ID_BRS_DATA_COMPARISON)
    public Result comparisonJob(Job job) {
        try {
            orderComparisonBService.compute(EventTypeEnum.COMPARISON.getTypeId());
            return new Result(Action.EXECUTE_SUCCESS, "订单人单比监控成功");
        }catch (Exception e){
            log.error("[{}], #JOB#TASK_ID_BRS_DATA_COMPARISON#  订单人单比监控失败 failed reason: {}", TAG, e);
            return new Result(Action.EXECUTE_FAILED, " 订单人单比监控失败");
        }
    }

    /**
     * 描述: 整体单量消息推送任务初始化
     * @param job 任务
     * @return com.github.ltsopensource.tasktracker.Result
     * @author chongw
     * @since 2021/06/28 16:12
     */
    @JobRunnerItem(shardValue = TaskConst.TASK_ID_BRS_DATA_QUANTITY_PUSH)
    public Result quantityPushJob(Job job) {
        try {
            orderQuantityBService.pushJob(EventTypeEnum.QUANTITY.getTypeId());
            return new Result(Action.EXECUTE_SUCCESS, "整体单量消息推送任务成功");
        }catch (Exception e){
            log.error("[{}], #JOB#TASK_ID_BRS_DATA_QUANTITY_PUSH#  整体单量消息推送任务失败 failed reason: {}", TAG, e);
            return new Result(Action.EXECUTE_FAILED, " 整体单量消息推送任务失败");
        }
    }

}
